/**
 * @Title: StringUtils
 * @Package cn.com.artemis.biz.steinsggs.common.util
 * @Description: TODO
 * @author yulin.xu@downjoy.com
 * @date 2017/7/19 17:44
 * @version V1.0
 */
package cn.com.artemis.biz.steinsggs.common.util;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 *@ClassName: StringUtils
 * @Description: TODO
 * @author yulin.xu@downjoy.com
 * @date 2017/7/19 17:44
 *
 */
public class StringUtils {

    private static final String NUMBER_POOL = "0123456789";
    private static final String CHAR_POOL = "abcdefghijklmnopqrstuvwxyz0123456789";

    public static final char UNDERLINE='_';
    public static String camelToUnderline(String param){
        if (param==null||"".equals(param.trim())){
            return "";
        }
        int len=param.length();
        StringBuilder sb=new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c=param.charAt(i);
            if (Character.isUpperCase(c)){
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            }else{
                sb.append(c);
            }
        }
        return sb.toString();
    }
    public static String underlineToCamel(String param){
        if (param==null||"".equals(param.trim())){
            return "";
        }
        int len=param.length();
        StringBuilder sb=new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c=param.charAt(i);
            if (c==UNDERLINE){
                if (++i<len){
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            }else{
                sb.append(c);
            }
        }
        return sb.toString();
    }
    public static String underlineToCamel2(String param){
        if (param==null||"".equals(param.trim())){
            return "";
        }
        StringBuilder sb=new StringBuilder(param);
        Matcher mc= Pattern.compile("_").matcher(param);
        int i=0;
        while (mc.find()){
            int position=mc.end()-(i++);
            //String.valueOf(Character.toUpperCase(sb.charAt(position)));
            sb.replace(position-1,position+1,sb.substring(position,position+1).toUpperCase());
        }
        return sb.toString();
    }

    public static String captureName(String name) {
        //     name = name.substring(0, 1).toUpperCase() + name.substring(1);
        //        return  name;
        char[] cs=name.toCharArray();
        cs[0]-=32;
        return String.valueOf(cs);

    }

    public static String int2Str(int value, int length) {
        StringBuilder sb = new StringBuilder();
        String str = value + "";
        if (str.length() >= length) {
            sb.append(str.substring(0, length));
        } else {
            for (int i = 0; i < length - str.length(); i++) {
                sb.append("0");
            }
            sb.append(str);
        }
        return sb.toString();
    }

    private static String getRandomString(String base, int length) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String getRandomString(int length) { //length表示生成字符串的长度
        return getRandomString(CHAR_POOL, length);
    }

    public static String getRandomNumberString(int length) { //length表示生成字符串的长度
        return getRandomString(NUMBER_POOL, length);
    }

    public static void main(String[] args) {
//        System.out.println(int2Str(13, 3));
        for (int i = 0; i < 100; i++) {
            System.out.println(getRandomNumberString(8));
        }
    }

}
